package com.jp.fm.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jp.fm.data.Budget;

public class SaveBudgets extends ServletParent {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public SaveBudgets() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public final void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public final void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {

		doPost(request, response);
	}

	public final void doPost(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {

		List<Integer> idsFromPage = new ArrayList<Integer>();

		// Build list of ids and names from the page
		Enumeration<String> paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {

			String name = paramNames.nextElement();

			if (name.startsWith("budget_type_")) {

				String counter = name.substring(name.lastIndexOf('_') + 1);
				String type = request.getParameter(name);
				String id = request.getParameter("budget_id_" + counter);

				if (null == id) {
					// insert a new record
					Float amount = Float.valueOf(request
							.getParameter("budget_amount_" + counter));

					Integer categoryId = Integer.parseInt(request
							.getParameter("budget_category_" + counter));
					String repeat = request.getParameter("budget_repeat_"
							+ counter);

					Budget budget = new Budget(null, new Date(), amount,
							repeat, type, getCategoryDao().findById(categoryId));

					int newId = getBudgetDao().writeObject(budget);
					budget.setId(newId);

					// Find the newly added BR and add its ID to the page list
					// so it does not
					// get deleted below.
					idsFromPage.add(budget.getId());
				}
			}

			if (name.startsWith("budget_id_")) {
				idsFromPage.add(Integer.parseInt(request.getParameter(name)));
			}
		}

		// do deletes by getting all budgets from the DB and iterating
		// over them.
		// If a budget id does not exist in the request then remove it from
		// the DB.
		List<Budget> dbBudgets = getBudgetDao().findByDate(new Date());
		for (Iterator<Budget> iterator = dbBudgets.iterator(); iterator
				.hasNext();) {
			Budget dbBudget = iterator.next();

			if (!idsFromPage.contains(dbBudget.getId())) {
				// Delete it
				getBudgetDao().delete(dbBudget);
			}
		}

		RequestDispatcher rd = request.getRequestDispatcher("/");
		rd.forward(request, response);
	}
}
